/**
 *
 */
package com.yogee.topseller.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.yogee.topseller.MainActivity;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.BeginQueryActivity;
import com.yogee.topseller.activity.DingyueListActivity;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.activity.PushProductListActivity;
import com.yogee.topseller.activity.SubscriptionActivity;
import com.yogee.topseller.activity.my.NoticeActivity;
import com.yogee.topseller.activity.my.OrderDetailsActivity;
import com.yogee.topseller.activity.my.PersonalDataActivity;
import com.yogee.topseller.bean.PushBean;


public class ShowNotification {

    private Notification notification;
    private NotificationManager nManager;
    private Intent mainIntent;


    /**
     * 推送类型 0:产品推送 1:产品列表 2:系统通知 3:认购 4:订单 5.订阅
     */

    public void showIsAppLive(Context context, String title, int id,
                              PushBean bean, String showType, String arg1, String arg2, String arg3) {
        nManager = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);

        if (bean.getPushType().equals("0")) {//产品
            mainIntent = new Intent(context, ProductDetailsActivity.class).putExtra("id", bean.getId()).putExtra("isFast", "0");
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else if (bean.getPushType().equals("2")) {//系统推送
            mainIntent = new Intent(context, NoticeActivity.class);
            if (bean.getMessageType().equals("0")) {//系统消息
                mainIntent.putExtra("type", 0);
            } else if (bean.getMessageType().equals("1")) {//服务消息
                mainIntent.putExtra("type", 1);
            } else {//通知消息
                mainIntent.putExtra("type", 2);
            }
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else if (bean.getPushType().equals("3")) {//认购
            mainIntent = new Intent(context, SubscriptionActivity.class);
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else if (bean.getPushType().equals("4")) {//订单详情
            mainIntent = new Intent(context, OrderDetailsActivity.class).putExtra("proId", bean.getId());
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else if (bean.getPushType().equals("1")) {//推送列表
            mainIntent = new Intent(context, PushProductListActivity.class).putExtra("id", bean.getId());
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }else if(bean.getPushType().equals("5")){ // 订阅
            mainIntent = new Intent(context, DingyueListActivity.class);
            mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        // 创建一个PendingIntent，和Intent类似，不同的是由于不是马上调用，需要在下拉状态条出发的activity，所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
        PendingIntent pendingIntent = PendingIntent.getActivity(context, id,
                mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(bean.getContent())
                .setContentTitle(title)
                .setContentText(bean.getContent())
                .setContentIntent(pendingIntent)
                .setNumber(1).build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时，通知将被清除。

        //添加声音提示
        notification.defaults = Notification.DEFAULT_SOUND;
        // audioStreamType的值必须AudioManager中的值，代表着响铃的模式
        notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;

        notification.defaults = Notification.DEFAULT_VIBRATE;
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        long[] vibrate = {0, 100, 200, 300};
        notification.vibrate = vibrate;
        nManager.notify(id, notification);
    }

    public void showIsNotApplive(Context context, String title, int id,
                                 String text, String showType, String arg1, String arg2, String arg3) {

        Bitmap bit = BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo);

        nManager = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);

        Intent launchIntent = context.getPackageManager()
                .getLaunchIntentForPackage("com.yogee.aipinyou");
        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        // 创建一个PendingIntent，和Intent类似，不同的是由于不是马上调用，需要在下拉状态条出发的activity，所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
        PendingIntent pendingIntent = PendingIntent.getActivity(context, id,
                launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        notification = new Notification.Builder(context)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(text)
                .setContentTitle(title)
                .setContentText(text)
                .setContentIntent(pendingIntent)
                .setNumber(1).build(); // 需要注意build()是在API
        // level16及之后增加的，API11可以使用getNotificatin()来替代
        notification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时，通知将被清除。

        //添加声音提示
        notification.defaults = Notification.DEFAULT_SOUND;
        // audioStreamType的值必须AudioManager中的值，代表着响铃的模式
        notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;

        notification.defaults = Notification.DEFAULT_VIBRATE;
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        long[] vibrate = {0, 100, 200, 300};
        notification.vibrate = vibrate;

        nManager.notify(id, notification);
    }
}
